package com.sunseagear.wind.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.common.utils.UserUtils;
import com.sunseagear.wind.modules.sys.entity.Organization;
import com.sunseagear.wind.modules.sys.service.IOrganizationService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/sys/organization")
@Log(title = "部门管理")
@RequiredArgsConstructor
public class OrganizationController extends BaseBeanController {

    private final IOrganizationService organizationService;


    /**
     * 分页查询系统配置列表
     *
     * @param name 查询条件
     * @return 分页数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckLogin
    public R<List<Organization>> list(String name) {
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Organization::getTenantId, UserUtils.getTenantId())
                .like(ObjectUtils.isNotEmpty(name),Organization::getName, name);
        List<Organization> list = organizationService.selectTreeList(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加
     * @param entity 前端提交的实体数据
     * @return 返回添加结果（成功or失败）
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sys:organization:add")
    public R<Organization> add(@Validated @RequestBody Organization entity) {
        entity.setTenantId(UserUtils.getTenantId());
        organizationService.save(entity);
        return R.success("添加成功", entity);
    }

    /**
     * 修改
     * @param entity 前端提交的实体数据
     * @return 返回修改结果（成功or失败）
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("sys:organization:update")
    public R<Void> update(@Validated @RequestBody Organization entity) {
        organizationService.updateById(entity);
        return R.success("更新成功");
    }

    /**
     * 详情
     * @param id 主键id
     * @return 详情数据
     */
    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:organization:detail")
    public R<Organization> detail(@PathVariable("id") Long id) {
        Organization organization = organizationService.getById(id);
        return R.success(organization);
    }

    /**
     * 删除
     * @param idList 主键id
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sys:organization:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        organizationService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
